/*
    Used to run a method from a thread other than the main one
    see http://eric.extremeboredom.net/2004/12/25/113
    Copyright (C) 2008 Bob Mottram
    fuzzgun@gmail.com

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using GtkSharp;
using System;
using System.Reflection;

namespace surveyor.vision
{
    public class RunOnMainThread 
    {
    	private object methodClass;
    	private string methodName;
    	private object[] arguments;

    	public static void Run(object methodClass, string methodName, object[] arguments) 
    	{
    		new RunOnMainThread(methodClass, methodName, arguments);
    	}

    	public RunOnMainThread(object methodClass, string methodName, object[] arguments) {
    		this.methodClass = methodClass;
            this.methodName = methodName;
    		this.arguments = arguments;
    		GLib.Idle.Add(new GLib.IdleHandler(Go));
    	}
    	private bool Go() 
    	{
    		methodClass.GetType().InvokeMember(methodName, BindingFlags.Default | BindingFlags.Public |
                BindingFlags.NonPublic | BindingFlags.InvokeMethod, null,methodClass, arguments);
    		return false;
    	}
    }
}
